﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using YouHP.Model;
using YouHP.DALFactory;
using YouHP.Utility;
using YouHP.BLL.MedicalGraph;

namespace YouHP.BLL
{
    public class PatientSymptomBLL
    {
        #region 单件模式
        static PatientSymptomBLL _Instance;

        public static PatientSymptomBLL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new PatientSymptomBLL();
                }
                return _Instance;
            }
        }
        #endregion

        #region 症状记录
        /// <summary>
        /// 取得病人的当前症状列表
        /// </summary>
        public List<SymptomHistoryInfo> GetCurrentSymptomList(int prId)
        {
            return DataAccess.PatientSymptomDAL.GetCurrentSymptomList(prId);
        }

        // 获取用户主要症状中最严重的一到两个；如果都是正常状态，随机选择一个；如果没有症状，返回空列表
        public List<SymptomHistoryInfo> GetPrimarySymptoms(int prId)
        {
            List<SymptomHistoryInfo> returnSymptomList = new List<SymptomHistoryInfo>();

            // 获得用户的所有症状
            List<SymptomHistoryInfo> symptomList = GetCurrentSymptomList(prId);

            // 如果用户没有任何症状，不要浪费时间了
            if (symptomList.Count == 0)
                return returnSymptomList;

            // 获得用户所患疾病对应的所有主要症状
            PatientRecordInfo prInfo = PatientBLL.Instance.GetPatientRecordInfoById(prId);
            List<DiseaseSymptomInfo> diseaseSymptomList
                = DiseaseBLL.Instance.GetDiseaseSymptomList(prInfo.DiseaseId, Constants.PRIMARY_SYMPTOM);

            foreach (SymptomHistoryInfo symptomHistInfo in symptomList)
            {
                if (symptomHistInfo.DegreeValue == Constants.SYMPTOM_NONE)  // 如果症状的程度为无，则在本轮中忽略之
                    continue;

                foreach (DiseaseSymptomInfo diseaseSymptomInfo in diseaseSymptomList)
                {
                    if (diseaseSymptomInfo.Type != Constants.PRIMARY_SYMPTOM)  // 主要症状
                        continue;

                    if (symptomHistInfo.SymptomId == diseaseSymptomInfo.SymptomId)
                    {
                        returnSymptomList.Add(symptomHistInfo);

                        if (returnSymptomList.Count >= 2)
                            return returnSymptomList;

                        break;
                    }
                }

            }

            if (returnSymptomList.Count > 0)
                return returnSymptomList;

            // 如果至此，说明用户没有主要症状，或者用户的主要症状与所患的疾病不对应
            foreach (SymptomHistoryInfo symptomHistInfo in symptomList)
            {
                if (symptomHistInfo.DegreeValue != Constants.SYMPTOM_NONE)
                {
                    returnSymptomList.Add(symptomHistInfo);
                    break;
                }
            }

            if (returnSymptomList.Count == 0)
                returnSymptomList.Add(symptomList[0]);

            return returnSymptomList;
        }

#if GENDATA
        public List<SymptomHistoryInfo> GetSymptomHistoryList(int prId, int symId, int pageSize, int pageIndex, out int count)
        {
            List<SymptomHistoryInfo> lst  = new List<SymptomHistoryInfo>();
            Random rnd = new Random();
            DateTime dt = Convert.ToDateTime("2006-05-09");
            for (int i = 0; i < 5; i++)
            {
                SymptomHistoryInfo info = new SymptomHistoryInfo();
                info.Id = i;
                info.SymptomId = i * 10;
                info.SymptomName = "症状 " + i;
                info.SurveyId = rnd.Next(1);
                info.RecordDate = dt.AddDays(i*49);;
                info.UpdateDate = DateTime.Now;
                info.DegreeValue = rnd.Next(3) + 1;
                lst.Add(info);
            }
            lst[0].SurveyId = 10;
            count = 102;
            return lst;
        }
#else
        /// <summary>
        /// 取得病人针对某种症状的症状记录历史
        /// </summary>
        public List<SymptomHistoryInfo> GetSymptomHistoryList(int prId, int symId, int pageSize, int pageIndex, out int count)
        {
            return DataAccess.PatientSymptomDAL.GetSymptomHistoryList(prId, symId, pageSize, pageIndex, out count);
        }
#endif

        /// <summary>
        /// 取得病人所有的症状记录历史（根据察看模式）
        /// </summary>
        public List<SymptomHistoryInfo> GetSymptomHistoryList(int prId, int viewMode)
        {
            DateTime minDate = new DateTime(1900, 1, 1);
            List<SymptomHistoryInfo> symLst = null;
            switch (viewMode)
            {
                case Constants.WEEKMODE:
                    break;
                case Constants.MONTHMODE:
                    break;
                case Constants.THREEMONTHMODE:
                    break;
                case Constants.YEARMODE:
                    break;
                case Constants.ALLMODE:
                default:
                    symLst = DataAccess.PatientSymptomDAL.GetSymptomHistoryList(prId, minDate, DateTime.Now);
                    break;
            }

            return symLst;
        }

        public List<SymptomHistoryInfo> GetSymptomHistoryList(int prId)
        {
            return DataAccess.PatientSymptomDAL.GetSymptomHistoryList(prId, new DateTime(1970,1,1), DateTime.Now);            
        }
        

        /// <summary>
        /// 取得病人的某种症状的当前程度
        /// </summary>
        public SymptomHistoryInfo GetCurrentSymptom(int prId, int symptomId)
        {
            return DataAccess.PatientSymptomDAL.GetCurrentSymptom(prId, symptomId);
        }

        // 获得PatientCurrentSymptom表中相关的记录
        public List<SymptomHistoryInfo> GetCurrentSymptomListBySymptomAndDegreeValue(int symptomId, int degreeValue)
        {
            return DataAccess.PatientSymptomDAL.GetCurrentSymptomListBySymptomAndDegreeValue(symptomId, degreeValue);
        }

        //判断历史记录表中是否已存在病人指定时间对应的记录
        public bool CheckSymptomHistory(int prId, int symptomId, DateTime recordDate)
        {
            return DataAccess.PatientSymptomDAL.CheckSymptomHistory(prId, symptomId, recordDate);
        }

        /// <summary>
        /// 添加单条的症状历史记录
        /// </summary>
        public bool AddSymptomHistory(SymptomHistoryInfo shInfo)
        {
            return DataAccess.PatientSymptomDAL.AddSymptomHistory(shInfo);
        }

        /// <summary>
        /// 根据历史Id获得症状历史记录信息
        /// </summary>
        public SymptomHistoryInfo GetSymptomHistory(int prId, int historyId)
        {
            return DataAccess.PatientSymptomDAL.GetSymptomHistory(prId, historyId);
        }

        /// <summary>
        /// 修改单条的症状历史记录
        /// </summary>
        public bool UpdateSymptomHistory(SymptomHistoryInfo shInfo)
        {
            return DataAccess.PatientSymptomDAL.UpdateSymptomHistory(shInfo);
        }

        /// <summary>
        /// 根据历史Id删除症状历史记录信息
        /// </summary>
        public bool DeleteSymptomHistory(int prId, int historyId)
        {
            return DataAccess.PatientSymptomDAL.DeleteSymptomHistory(prId, historyId);
        }

        /// <summary>
        /// 批量的症状历史记录提交
        /// </summary>
        public bool AddSymptomSurvey(List<SymptomHistoryInfo> shList)
        {
            return DataAccess.PatientSymptomDAL.AddSymptomSurvey(shList);
        }

        /// <summary>
        /// 获得批量的症状历史记录
        /// </summary>
        public List<SymptomHistoryInfo> GetSymptomSurvey(int prId, int surveyId)
        {
            return DataAccess.PatientSymptomDAL.GetSymptomSurvey(prId, surveyId);
        }

        /// <summary>
        /// 批量的症状历史记录修改
        /// </summary>
        public bool UpdateSymptomSurvey(int surveyId, List<SymptomHistoryInfo> shList)
        {
            return DataAccess.PatientSymptomDAL.UpdateSymptomSurvey(surveyId, shList);
        }

        /// <summary>
        /// 根据SurveyId删除症状历史记录信息
        /// </summary>
        public bool DeleteSymptomSurvey(int prId, int surveyId)
        {
            return DataAccess.PatientSymptomDAL.DeleteSymptomSurvey(prId, surveyId);
        }

        #endregion
    }
}
